package com.irdstudio.efp.esb.api.service.yed;

import com.irdstudio.efp.esb.api.rest.yed.resp.RepayDetailArray;
import com.irdstudio.efp.esb.api.service.sed.ploaninfo.PloaninfoUtils;
import com.irdstudio.efp.esb.api.util.RepayTypeTransUtil;
import com.irdstudio.efp.esb.service.bo.resp.hj.HjRpayDetaillInfArryNew;
import com.irdstudio.efp.esb.service.bo.resp.hj.RepayInfoQueryTotalRespBean;
import com.irdstudio.efp.loan.service.vo.LoanRepayDetailVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 需求编号：xxxxxx<br/>
 * 问题编号：<br/>
 * 功能描述：优e贷还款明细数据封装<br/>
 * 创建时间：2020/9/16 18:29
 *
 * @Author tiankg
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class YedRepayDetailTotalService {

    /**
     * 设置还款明细的数据
     */
    private RepayDetailArray c2DetailArr(LoanRepayDetailVO loanRepayDetailVO) {
        RepayDetailArray repayDetailArray = new RepayDetailArray();
        //还款申请流水号
        repayDetailArray.setRpymtAppSrlNo(loanRepayDetailVO.getSetlSeq());
        //剩余本金
        // repayDetailArray.setRmanPnp(loanRepayDetailVO.getPsRemPrcp());
        //还款期数
        repayDetailArray.setRpymtTrmCnt(loanRepayDetailVO.getRepayTerm());
        //还款类型
        repayDetailArray.setRpymtTp(loanRepayDetailVO.getSetlTyp());
        //执行利率
//        repayDetailArray.setExecIntRt(loanRepayDetailVO.getIntRat());
        //还款总计金额
        repayDetailArray.setRpymtTotAmt(loanRepayDetailVO.getTotalAmt());
        //归还本金
        repayDetailArray.setRpyblPnp(loanRepayDetailVO.getSetlOdPrcpAmt());
        //归还利息
        repayDetailArray.setRpyblInt(loanRepayDetailVO.getSetlPsIncTaken());
        //归还罚息
        repayDetailArray.setRpyblPnyInt(loanRepayDetailVO.getSetlOdIntAmt());
        //归还复利
        repayDetailArray.setRpyblCmpdInt(loanRepayDetailVO.getSetlOdCommInt());
        //归还复利的复利
        repayDetailArray.setRpyCmpdIntPrsnlCmpdInt(BigDecimal.ZERO);
        //归还罚息的复利
        repayDetailArray.setRpyPnyIntPrsnlCmpdInt(BigDecimal.ZERO);
        //归还费用
        repayDetailArray.setRpyblFee(loanRepayDetailVO.getSetlTotalFee());
        //还款账号
        repayDetailArray.setRpymtAcctNo(loanRepayDetailVO.getDisbAcNo());
        //还款状态
        repayDetailArray.setRpymtSt(loanRepayDetailVO.getRpSts());
        //还款日期
        repayDetailArray.setRpymtDt(loanRepayDetailVO.getSetlApplyDt());
        //审批状态wfApprSts
        repayDetailArray.setAprvlSt(loanRepayDetailVO.getWfApprSts());
        //失败原因refuseCause
        repayDetailArray.setFailRsn(loanRepayDetailVO.getRefuseCause());
        repayDetailArray.setCreateTime(loanRepayDetailVO.getSetlCreateDt());
        return repayDetailArray;
    }

    private RepayDetailArray c2DetailArr(HjRpayDetaillInfArryNew rpyDtlInfArry) {
        RepayDetailArray repayDetailArray = new RepayDetailArray();
        //还款申请流水号
        repayDetailArray.setRpymtAppSrlNo(rpyDtlInfArry.getRpyNo());
        //剩余本金
        //repayDetailArray.setPsPrin(loanRepayDetailVO.getPsRemPrcp());
        //还款期数
//        repayDetailArray.setSetlTerm(loanRepayDetailVO.getRepayTerm());
        //还款类型
       /* 网贷还款类型	    互金还款类型
        01 正常还款	        04-NS-正常
        02 提前部分还款	    02-ER-提前部分还本
        03 提前结清	        01-PO-全部结清
        04 逾期还款	        06-NSYUQ-逾期还款
        06 按期次提前还款	    05-PS-按期次提前还款
        10 部分还款（先还欠款如有剩余提前还本）	03-PR-部分还款
        11逾期结清           07-POYUQ po逾期结清*/
        repayDetailArray.setRpymtTp(RepayTypeTransUtil.transHj2Wd(rpyDtlInfArry.getRpyType()));
        //执行利率
//        repayDetailArray.setExecIntRt();
        //还款总计金额
        repayDetailArray.setRpymtTotAmt(rpyDtlInfArry.getRpyAmt());
        //归还本金
        repayDetailArray.setRpyblPnp(rpyDtlInfArry.getRcylPnpAmt());
        //归还利息
        repayDetailArray.setRpyblInt(rpyDtlInfArry.getRcylIntAmt());
        //归还罚息
        repayDetailArray.setRpyblPnyInt(rpyDtlInfArry.getRcylPnyIntAmt());
        //归还复利
        repayDetailArray.setRpyblCmpdInt(rpyDtlInfArry.getRcylCmpdIntAmt());
        //归还复利的复利
        repayDetailArray.setRpyCmpdIntPrsnlCmpdInt(rpyDtlInfArry.getRcylCmpdIntNrlCmpdIntAmt());
        //归还罚息的复利
        repayDetailArray.setRpyPnyIntPrsnlCmpdInt(rpyDtlInfArry.getRcylPnyIntNrlCmpdIntAmt());
        //归还费用
        repayDetailArray.setRpyblFee(PloaninfoUtils.getAddResult(rpyDtlInfArry.getRcylPnpAmt(), rpyDtlInfArry.getRcylIntAmt(), rpyDtlInfArry.getRcylCmpdIntAmt(), rpyDtlInfArry.getRcylPnyIntAmt(), rpyDtlInfArry.getRcylCmpdIntNrlCmpdIntAmt(), rpyDtlInfArry.getRcylPnyIntNrlCmpdIntAmt()));
        //还款账号
        repayDetailArray.setRpymtAcctNo("");
        if (rpyDtlInfArry.getRpyAmt() == null) {
            //还款状态(互金无返回)
            repayDetailArray.setRpymtSt("0");
            //审批状态wfApprSts(互金无返回)
            repayDetailArray.setAprvlSt("03");
        } else {
            //还款状态(互金无返回)
            repayDetailArray.setRpymtSt(rpyDtlInfArry.getRpyAmt().doubleValue() <= 0 ? "0" : "1");
            //审批状态wfApprSts(互金无返回)
            repayDetailArray.setAprvlSt(rpyDtlInfArry.getRpyAmt().doubleValue() <= 0 ? "03" : "02");
        }
        //还款日期
        repayDetailArray.setRpymtDt(rpyDtlInfArry.getRpyDt() == null ? "" : LocalDate.parse(rpyDtlInfArry.getRpyDt(), DateTimeFormatter.ofPattern("yyyyMMdd")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //失败原因refuseCause
        repayDetailArray.setFailRsn(rpyDtlInfArry.getErrDescInf());
        //应还日期
        repayDetailArray.setRpyblPymtDt(rpyDtlInfArry.getRpyblPymtDt());
        repayDetailArray.setCreateTime(rpyDtlInfArry.getTxnDt());
        return repayDetailArray;
    }

    /**
     * @param loanRepayDetailVOS     本地的还款记录失败数据
     * @param accLoanVO              本地的借据信息
     * @param repayInfoQueryRespBean 根据贷款发放号查询互金返回的贷款回收信息
     */
    public void handleResp(List<RepayDetailArray> repayDetailArrs, List<LoanRepayDetailVO> loanRepayDetailVOS, RepayInfoQueryTotalRespBean repayInfoQueryTotalRespBean) {
        //1.从互金接口取出还款明细 存于repayDetailHj
        List<RepayDetailArray> repayDetailHj = new ArrayList<RepayDetailArray>();
        repayInfoQueryTotalRespBean.getHjLoanArrys()
                                   .stream()
                                   .filter(h -> h.getRpyDtlInfArrys() != null)
                                   .flatMap(h -> h.getRpyDtlInfArrys().stream())
                                   .forEach(r -> repayDetailHj.add(c2DetailArr(r)));

        // 2.从本地取失败的记录，根据还款号与互金返回的还款明细去重后 存于repayDetailLocal
        List<RepayDetailArray> repayDetailLocal = new ArrayList<RepayDetailArray>();
        if (Objects.nonNull(loanRepayDetailVOS) && loanRepayDetailVOS.size() > 0) {
           loanRepayDetailVOS.stream().forEach(r -> repayDetailLocal.add(c2DetailArr(r)));
        }

        // 3.互金与网贷本地还款数据合并
        repayDetailArrs.addAll(repayDetailHj);
        repayDetailArrs.addAll(repayDetailLocal);

    }
}
